package net.huashitong.supervision.task.dao;

import com.alibaba.druid.sql.visitor.functions.If;
import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.task.entity.TaskUser;
import net.huashitong.supervision.task.filter.TaskUserFilter;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/8/6
 * Time: 17:56
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Repository
public class TaskUserDao extends StringDao<TaskUser> {
    /**
     * 查询单位任务分配人员
     * @param taskUnitId
     * @return
     */
    public List<TaskUser> getByTaskUnitId(String taskUnitId) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null ");
        query.append(" and taskUnitId =:taskUnitId").setParam("taskUnitId",taskUnitId );
        return query.listResult();
    }

    public List<TaskUser> getByTaskUnitIdDel(String taskUnitId) {
        GenericQuery query = createQuery("obj");
        query.append(" and del =:del ").setParam("del","0" );
        query.append(" and taskUnitId =:taskUnitId").setParam("taskUnitId",taskUnitId );
        return query.listResult();
    }

    public List<TaskUser> queryFilter(TaskUserFilter filter){
        GenericQuery query = createQuery("obj");
        query.append(" and del is null ");

        if (StringUtils.isNotBlank(filter.getTaskSubId())){
            query.append(" and taskSubId =:taskSubId").setParam("taskSubId",filter.getTaskSubId());
        }
        if (StringUtils.isNotBlank(filter.getTaskUnitId())){
            query.append(" and taskUnitId =:taskUnitId").setParam("taskUnitId",filter.getTaskUnitId() );
        }
        if (StringUtils.isNotBlank(filter.getUnitUserId())&&StringUtils.isBlank(filter.getCreateUser())){
            query.append(" and unitUserId =:unitUserId").setParam("unitUserId",filter.getUnitUserId() );
        }
        if (StringUtils.isNotBlank(filter.getCreateUser())&&StringUtils.isBlank(filter.getUnitUserId())){
            query.append(" and createUser =:createUser").setParam("createUser",filter.getCreateUser() );
        }
        if(StringUtils.isNotBlank(filter.getUnitUserId())&&StringUtils.isNotBlank(filter.getCreateUser()))
        {
            query.append(" and  ( createUser =:createUser").setParam("createUser",filter.getCreateUser() )
            .append(" or unitUserId =:unitUserId )").setParam("unitUserId",filter.getUnitUserId() );

        }
        if (StringUtils.isNotBlank(filter.getStatus())){
            if("1".equals(filter.getStatus())){
                query.append(" and (status ='1' or status='5' or status='3' ) " );
            }else {

                query.append(" and status =:status").setParam("status",filter.getStatus() );
            }
        }
        if(1==filter.getImp())
        {

            query.append(" and imp= 1  ");
        }else {
            query.append(" and  ( imp = 0 or imp =2 )");
        }
        query.setOrder(" updDate",GenericQuery.DESC);
        return query.listResult(filter);
    }

    public List<TaskUser> getByCreateUserAndTaskUnitId(String userId,String taskUnitId) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null ");

        if (StringUtils.isNotBlank(userId)){
            query.append(" and createUser =:createUser").setParam("createUser",userId);
        }
        if (StringUtils.isNotBlank(taskUnitId)){
            query.append(" and taskUnitId =:taskUnitId").setParam("taskUnitId",taskUnitId);
        }
        return query.listResult();
    }

    public List<TaskUser> getByTaskUnitIdAndUnitUserId(String taskUnitId, String userId) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null ");

        if (StringUtils.isNotBlank(userId)){
            query.append(" and unitUserId =:unitUserId").setParam("unitUserId",userId);
        }
        if (StringUtils.isNotBlank(taskUnitId)){
            query.append(" and taskUnitId =:taskUnitId").setParam("taskUnitId",taskUnitId);
        }
        return query.listResult();
    }

    public void deleteByUnitIds(List<String> taskUnitIds) {
        StringBuffer hql = (new StringBuffer("delete from TaskUser")).append(" obj where obj.taskUnitId in(:taskUnitIds)");
        Query query = this.getSession().createQuery(hql.toString());
        query.setParameterList("taskUnitIds", taskUnitIds);
        query.executeUpdate();
    }

    public List<TaskUser> queryStatistics(TaskUserFilter filter) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null ");

        if (StringUtils.isNotBlank(filter.getTaskSubId())){
            query.append(" and taskSubId =:taskSubId").setParam("taskSubId",filter.getTaskSubId());
        }
        if (StringUtils.isNotBlank(filter.getTaskUnitId())){
            query.append(" and taskUnitId =:taskUnitId").setParam("taskUnitId",filter.getTaskUnitId() );
        }
        if (StringUtils.isNotBlank(filter.getUnitUserId())){
            query.append(" and unitUserId =:unitUserId").setParam("unitUserId",filter.getUnitUserId() );
        }
        if (StringUtils.isNotBlank(filter.getStatus())){
            query.append(" and status =:status").setParam("status",filter.getStatus() );
        }
        if (StringUtils.isNotBlank(filter.getOverdueDay())) {
            query.append(" and overdueDay is not null " );
        }
        if (StringUtils.isNotBlank(filter.getBackNum())) {
            query.append(" and backNum is not null " );
        }
        //查询的创建时间.条件只会在月,当数据创建时间与查询月份相同时
        if (StringUtils.isNotBlank(filter.getCreateDate())) {
            query.append(" and createDate like:createDate").setParam("createDate",StringUtils.getLikewords( filter.getCreateDate()));
        }
        if (StringUtils.isNotBlank(filter.getUpdDate())) {
            query.append(" and updDate like:updDate").setParam("updDate",StringUtils.getLikewords( filter.getUpdDate()));
        }
        return query.listResult(filter);
    }
}
